#[allow(dead_code)]
#[derive(Copy,Clone)]
struct Book {
    // 只读的引用
    // static lifetime which means the reference is valid for the entire program execution
    // `&'static str` is a reference to a string allocated in read only memory
    author: &'static str,
    title: &'static str,
    year: u32,
}

// 不可变借用
fn borrow_book(book: &Book) {
    println!("I am immutable borrowing the book: title: {}, year: {}, author: {}", book.title, book.year, book.author);
}

// 可变借用
fn new_edition(book: &mut Book) {
    book.year = 2024;
    println!("I am mutable borrowing the book: title: {}, year: {}, author: {}", book.title, book.year, book.author);
}

fn main() {
    let immutable_book = Book {
        author: "Alice",
        title: "The Girl with the Dragon Tattoo",
        year: 1984,
    };

    // create a mutable 【copy】 of the immutable book
    let mut mutable_book = immutable_book;

    // immutable borrowing
    borrow_book(&immutable_book);
    // 不可变借用，也可以使用可变引用作为参数
    borrow_book(&mutable_book);

    // mutable borrowing
    new_edition(&mut mutable_book);

    // new_edition(&mut immutable_book); // error: cannot borrow `immutable_book` as mutable because it is also borrowed as immutable
}